package com.example.demo.service.fuqiang;

import com.example.demo.dao.mapper.SysSessionMapper;
import com.example.demo.dao.model.SysSession;
import com.example.demo.dao.model.SysSessionCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class SessionService implements ISessionService {
	@Autowired
	private SysSessionMapper sessionMapper;

	@Override
	public SysSession selectOne(String sessionId) {
		return sessionMapper.selectByPrimaryKey(sessionId);
	}

	@Override
	public List<SysSession> selectMany(SysSession criteria) {

		SysSessionCriteria sysCriteria = new SysSessionCriteria();
		sysCriteria.createCriteria().andDelFlagEqualTo(0);
		return sessionMapper.selectByCriteria(sysCriteria);
	}

	@Override
	public Boolean insertSession(SysSession session) {
		session.setSessionId(UUID.randomUUID().toString());
		session.setUserId("0");
		session.setCreateBy("fuqiang");
		session.setUpdateBy("fuqiang");
		long time = System.currentTimeMillis();
		session.setCreateTime(new Date(time));
		session.setUpdateTime(new Date(time));
		session.setExpireTime(new Date(time + (12*60*60*1000)));
		int count = sessionMapper.insertSelective(session);

		return count > 0;
	}

	@Override
	public Boolean updateSession(SysSession session) {
		session.setUpdateBy("fuqiang");
		long now =System.currentTimeMillis();
		session.setUpdateTime(new Date(now));
		int count = sessionMapper.updateByPrimaryKeySelective(session);
		return count > 0;
	}
}
